﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using NPOI.HSSF.UserModel;
using Ext.Net;

namespace Taxes.Caculate
{
    public partial class RegisterImport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [DirectMethod]
        public void DeleteFile(string fileName, string fileClientName)
        {
            try
            {
                System.IO.File.Delete(fileName);
                //X.Msg.Notify("提示", fileClientName + "已删除!").Show();
            }
            catch (Exception ex)
            {
                //X.Msg.Notify("提示", fileClientName + "删除失败!").Show();
            }
        }
        [DirectMethod]
        public void Up(string att)
        {
            var path = System.Configuration.ConfigurationManager.AppSettings["UploadPath"].ToString();
            var fileName = Server.MapPath(path + "/" + att);
            Import(fileName);

        }
        private void Import(String path)
        {
            var revenue_infos = new List<Model.revenue_info>();
            using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                var workBook = new HSSFWorkbook(stream);
                var enumerator = workBook.GetEnumerator();
               
                while (enumerator.MoveNext())
                {
                    var sheet = (HSSFSheet)enumerator.Current;
                    if (sheet == null) continue;
                    var rowEnumerator = sheet.GetEnumerator();
                    while (rowEnumerator.MoveNext())
                    {
                        var row = (HSSFRow)rowEnumerator.Current;
                        if (row == null)
                            break;
                        if (row.RowNum < 1)
                            continue;
                        Model.revenue_info info = new Model.revenue_info();
                        var cellEnumerator = row.GetEnumerator();
                        while (cellEnumerator.MoveNext())
                        {
                            var cell = cellEnumerator.Current as HSSFCell;
                            if (cell == null) continue;
                            if (cell.ColumnIndex == 0)
                            {
                                int value ;
                                if (!int.TryParse(cell.StringCellValue, out value))
                                {
                                    break;
                                }
                            }
                            switch (cell.ColumnIndex)
                            {
                                case 1:
                                    info.PC_Code = cell.StringCellValue;
                                    break;
                                case 2:
                                    info.reve_name = cell.StringCellValue;
                                    break;
                                case 3:
                                    info.reve_code = cell.StringCellValue;
                                    break;
                                case 4:
                                    switch (cell.CellType)
                                    {
                                        case  NPOI.SS.UserModel.CellType.STRING:
                                            info.Register_Date = DateTime.Parse(cell.StringCellValue);
                                            break;
                                    }
                                    break;
                                case 5:
                                    info.Register_type = cell.StringCellValue;
                                    break;
                                case 6:
                                    info.Industry= cell.StringCellValue;
                                    break;
                                case 7:
                                    info.BigIndustry = cell.StringCellValue;
                                    break;
                                case 8:
                                    info.Register_status = cell.StringCellValue;
                                    break;
                                case 9:
                                    break;
                                case 10:
                                    info.Operate_Person = cell.StringCellValue;
                                    break;
                            }
                        }
                        if (!String.IsNullOrEmpty(info.reve_code))
                        {
                            revenue_infos.Add(info);
                        }
                    }
                }

            }

            ///进库.
            var bll = new BLL.revenue_info();
            //得到所有的用户
            var userBLL = new BLL.ysys_user();
            var users = userBLL.GetModelList("");
            ///得到所有的行业大类
            var industryBLL = new BLL.industry_info();
            var industries = industryBLL.GetAllBigIndustry();
            foreach (var info in revenue_infos)
            {
                foreach (var user in users)
                {
                    if (info.Operate_Person == user.user_tname)
                    {
                        info.reve_operator = user.ID;
                        break;
                    }
                }
                foreach (var i in industries)
                {
                    if (info.BigIndustry == i.industry_name)
                    {
                        info.industry_id = i.ID;
                        break;
                    }
                }
                bll.Add(info);
            }

            X.Msg.Show(new MessageBoxConfig() { Buttons = MessageBox.Button.OK, Title = "提示", Message = "导入成功" });
        }
    }
}